package com.yijiaganxi.member;

import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {

	static Lock lock=new ReentrantLock(true);
	
	public static void main(String[] args) {

		
		Condition condition = lock.newCondition();
		AtomicInteger num=new AtomicInteger(10);
		ConcurrentHashMap<Integer, Integer> map=new ConcurrentHashMap();
		for(int i=0;i<10;i++) {
			new Thread(()->{
				int nextInt = new Random().nextInt();
				map.put(nextInt,nextInt);
				
				try {
					lock.lock();
					num.decrementAndGet();
					condition.signal();
				} finally {
					// TODO: handle finally clause
					lock.unlock();
				}
			}) .start();
		}
		
		try {
			lock.lock();
			while(num.get()>0) {
				condition.await();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			lock.unlock();
		}
		System.out.println(map);
	}
}
